package com.neu.util;

import java.sql.*;
import static com.neu.util.ReadUtil.*;//静态引入无需每次引用；

public final class DBUtil {
    private static final String driver = getDriver();
    private static final String url = getUrl();
    private static final String user = getUser();
    private static final String password = getPassword();


    static{
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {

            //把异常信息抛出
            throw new RuntimeException(e);
        } finally {

        }
    }
    private DBUtil(){

    }
     public static Connection getConnection() throws SQLException {
         Connection conn = DriverManager.getConnection(url,user,password);
         return conn;
     }
    public static void close(ResultSet rst,PreparedStatement pstm,Connection conn){

        if (rst!=null){
            try {
                rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                rst=null;
            }
        }
        if (pstm!=null){
            try {
                pstm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                pstm=null;
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
                conn=null;
            }
        }
    }
}
